<template>
  <div class="container">
    <div class="visitor-page">
      <div class="body">
        <ul class="list" v-if="list.length > 0">
          <li class="item" v-for="(item, index) in list" :key="index" v-if="item.id != userInfo.id" @click="router(item.id)">
            <div class="avatar">
              <img class="image" :src="item.avatarUrl" alt="">
            </div>
            <div class="user">
              <div class="nick">{{ item.nickName || item.name }}</div>
              <div class="job">{{ item.company.title ? item.company.title : '' }}</div>
            </div>
            <div class="content">{{ item.update_time }}</div>
          </li>
          <li class="nomore" v-if="isLock">我是有底线的</li>
        </ul>
        <div class="data-empty" v-else>
          <image class="icon-empty" src="/static/images/icon/empty.png" alt=""/>
        </div>
      </div>
    </div>
  </div>
</template>

<script>

export default {
  data () {
    return {
      list: [],
      page: 1,
      isLock: false,
    }
  },

  onShareAppMessage (res) {
    let self = this;
    let url = `${self.$confs.shareUrl}?id=${self.userInfo.id}`;
    return {
      title: self.$confs.shareTitle,
      path: url,
    }
  },

  computed: {
    userInfo () {
      return this.$store.state.userInfo;
    },
  },

  methods: {
    /*
     * 获取访客列表
     */
    getVisitor () {
      let self = this;
      this.$toast.show();
      this.$axios.visitors({
        id: self.userInfo.id
      }).then((res) => {
        self.$toast.hide();
        let data = res.data;
        if (0 === data.code * 1) {
          self.list = data.data;
        }
      });
    },
    router (id) {
      let url = `/pages/guest/main?id=${id}`;
      wx.navigateTo({url});
    },
  },
  /*
  * 页面上拉触底事件的处理函数
  */
  onReachBottom: function () {
    let self = this;
    if (!self.isLock) {
      self.$toast.show('玩命加载中');
      self.page += 1;
      self.$axios.visitors({
        id: self.userInfo.id,
        page: self.page,
      }).then(res=>{
        self.$toast.hide();
        let data = res.data;
        if (0 === data.code * 1) {
          if (res.data.data.length > 0) {
            self.list.push(...data.data);
          } else {
            self.isLock = true;
          }
        }else {
          self.$modal.show('数据加载失败!');
        }
      })
    }
  },

  mounted () {
    this.getVisitor();
  }
}
</script>

<style lang="scss">
@import './index.scss';
</style>
